package com.xinpu.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinpu.entity.TCompany;
import com.xinpu.service.ITCompanyInfoService;
import com.xinpu.service.ITCompanyService;
import com.xinpu.service.OwnService;
import com.xinpu.util.PasswordUtils;
import com.xinpu.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/companyInfo")
public class CompanyInfoCintroller {
    @Autowired
    private ITCompanyService itCompanyService;

    @GetMapping("/info")
    public ResponseMessage info() {
        QueryWrapper<TCompany> wrapper = new QueryWrapper<>();
        wrapper.eq("tenant_id", OwnService.getLoginer().getTenantId());
        return ResponseMessage.ok(itCompanyService.list(wrapper));
    }

    /**
     * 编辑企业信息
     *
     * @param map
     * @return
     */
    @PostMapping("/update")
    public ResponseMessage update(@RequestBody Map<String, String> map) {
        TCompany tCompany = itCompanyService.getById(OwnService.getCurrentUserID());
        tCompany.setAddress(map.get("address"));
        tCompany.setAddressInfo(map.get("addressInfo"));
        tCompany.setCode(map.get("code"));
        tCompany.setIntro(map.get("intro"));
        tCompany.setName(map.get("name"));
        tCompany.setPhone(map.get("phone"));
        tCompany.setPortrait(map.get("portrait"));
        tCompany.setUserName(map.get("userName"));
        return ResponseMessage.ok(itCompanyService.updateById(tCompany));
    }

    /**
     * 修改密码
     *
     * @param map
     * @return
     */
    @PostMapping("/editPassword")
    public ResponseMessage editPassword(@RequestBody Map<String, String> map) {
        TCompany tCompany = itCompanyService.getById(OwnService.getCurrentUserID());
        String salt = RandomUtil.randomString(6);
        String pwd = PasswordUtils.generatePwd(salt, map.get("password"));
        tCompany.setPassword(pwd);
        tCompany.setSalt(salt);
        itCompanyService.updateById(tCompany);
        return ResponseMessage.ok();
    }


}
